package zack.gateway.core.bind;

import zack.gateway.core.mapping.HttpStatement;
import zack.gateway.core.session.Configuration;
import zack.gateway.core.session.GatewaySession;

import java.util.HashMap;
import java.util.Map;

/**泛化调用注册机，一个uri对应一个代理工厂
 * @author A1793
 */
public class MapperRegistry {
    private Configuration configuration;
    public MapperRegistry(Configuration configuration) {
        this.configuration = configuration;
    }
    private final Map<String,MapperProxyFactory> knownMappers = new HashMap<String,MapperProxyFactory>();

    public IGenericReference getMapper(String uri, GatewaySession session) {
        MapperProxyFactory mapperProxyFactory = knownMappers.get(uri);
        if(mapperProxyFactory == null) {
            throw new RuntimeException("Uri " + uri + " is not known to the MapperRegistry.");
        }
        try{
            return mapperProxyFactory.newInstance(session);
        }catch(Exception e){
            throw new RuntimeException("Error getting mapper instance. Cause: " + e, e);
        }
    }

    /**
     * 注册http调用信息，
     * @param statement
     */
    public void addMapper(HttpStatement statement) {
        String uri = statement.getUri();
        if(knownMappers.containsKey(uri)) {

        }
        knownMappers.put(uri,new MapperProxyFactory(uri));
        configuration.addHttpStatement(statement);

    }
}
